﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BaHuan.BusinessLayer;
using BaHuan.Common;
using BaHuan.Entities;

namespace BaHuan
{
    public partial class FrmPhieuNhapKho : Form
    {
        #region Khai bao cuc bo
        private ChiNhanhCongTyMgt chinhanhctyMgt = new ChiNhanhCongTyMgt();
        private BangKeNhapHangMgt bangkenhaphangMgt = new BangKeNhapHangMgt();
        private PhieuNhapKhoMgt phieunhapkhoMgt = new PhieuNhapKhoMgt();
        private CTBangKeNhapHangMgt ctbankenhaphangMgt = new CTBangKeNhapHangMgt();
        private HangHoaManagement hanghoaMgt = new HangHoaManagement();
        private CTPhieuNhapKhoMgt ctphieunhapkhoMgt = new CTPhieuNhapKhoMgt();
        private QuanLyKhoManagement quanlykhoMgt = new QuanLyKhoManagement();
        private KhoManagement khoMgt = new KhoManagement();
        private ButToanMgt buttoanMgt = new ButToanMgt();
        private MuaHangManagement muahangMgt = new MuaHangManagement();
        private CTMuaHangManagement ctmuahangMgt = new CTMuaHangManagement();

        private List<CTPhieuNhapKho> lstDeleteCTPhieuNhapKho = new List<CTPhieuNhapKho>();
        private PhieuNhapKho phieunhapkho;
        private bool IsEdited = false;
        private bool IsEdit = true;
        public long tien = 0;
        public int IdBK = 0;

        private NhanVien nhanvien;
        public NhanVien NhanVien
        {
            set { this.nhanvien = value; }
            get { return this.nhanvien; }
        }

        private ChiNhanhCongTy chinhanhcongty;
        public ChiNhanhCongTy ChiNhanhCongTy
        {
            set { this.chinhanhcongty = value; }
            get { return this.chinhanhcongty; }
        }

        private BangKeNhapHang bangkenhaphang;
        public BangKeNhapHang BangKeNhapHang
        {
            set { this.bangkenhaphang = value; }
            get { return this.bangkenhaphang; }
        }

        private MuaHang muahang;
        public MuaHang Muahang
        {
            set { this.muahang = value; }
            get { return this.muahang; }
        }
        #endregion

        #region Xu ly Su kien load
        public FrmPhieuNhapKho(BangKeNhapHang bangkenhaphang, NhanVien nv)
        {
            InitializeComponent();

            btnSua.Enabled = false;
            btnXoa.Enabled = false;
            btnDinhKhoan.Enabled = false;
            btnIn.Enabled = false;

            this.BangKeNhapHang = bangkenhaphang;
            this.NhanVien = nv;
            txtNoiDung.Focus();

            cbKhoNhap.DataSource = GetAllChiNhanhCongTy();
            cbKhoNhap.DisplayMember = "TenChiNhanh";
            cbKhoNhap.ValueMember = "Id";

            txtNguoiLap.Text = this.nhanvien.HoTen;
            txtKeToanTruong.Text = this.bangkenhaphang.KeToanTruong;
            txtSoPhieu.Text = GetMaSoPhieuNhapKho();
            txtNguoiGiaoHang.Text = this.bangkenhaphang.NguoiGiaoHang;
            txtSoHoaDon.Text = this.bangkenhaphang.MaSo;
            txtNguoiGiaoHang.Enabled = false;
            txtKeToanTruong.Enabled = false;
            txtNguoiLap.Enabled = false;
            IdBK = this.bangkenhaphang.Id;
        

            dgvDanhSachPhieuNhapKho.Rows.Clear();
            cbKhoNhap.SelectedIndex = 0;
            LoadDataGridView();
            IsEdit = false;            
        }

        public FrmPhieuNhapKho(PhieuNhapKho phieunhapkho, NhanVien nv)
        {
            InitializeComponent();

            dgvDanhSachPhieuNhapKho.AutoGenerateColumns = false;
            dgvDanhSachPhieuNhapKho.ReadOnly = true;

            this.NhanVien = nv;
            this.phieunhapkho = phieunhapkho;

            ResultObj rs = new ResultObj();
            rs = chinhanhctyMgt.GetAllChiNhanhCongTy();

            ChiNhanhCongTy chinhanh = (from cn in (List<ChiNhanhCongTy>)rs.Result
                                             where cn.Id == this.phieunhapkho.IdChiNhanhCTy &&
                                             cn.IsDelete==0
                                             select cn).FirstOrDefault();
            btnLuu.Enabled = false;
            gbPhieuNhapKho.Enabled = false;

            txtSoPhieu.Text = this.phieunhapkho.MaSo;
            dtpNgayThang.Value = this.phieunhapkho.NgayThang;
            txtNguoiLap.Text = this.phieunhapkho.NguoiLap;
            txtNoiDung.Text = this.phieunhapkho.NoiDung;
            txtNguoiGiaoHang.Text = this.phieunhapkho.NguoiGiaoHang;
            txtKeToanTruong.Text = this.phieunhapkho.KeToanTruong;
            txtSoHoaDon.Text = this.phieunhapkho.SoHoaDonDo;
            txtVAT.Text = this.phieunhapkho.Thue.ToString();
            lblId.Text = this.phieunhapkho.Id.ToString();
            cbKhoNhap.Text = chinhanh.TenChiNhanh;
            IdBK = this.phieunhapkho.IdBangKe;

            rs = ctphieunhapkhoMgt.GetAllCTPhieuNhapKho();
            List<CTPhieuNhapKho> lstCTPhieuNhapKho = (from ct in (List<CTPhieuNhapKho>)rs.Result
                                                      where ct.IdPhieuNhapKho == phieunhapkho.Id&&
                                                      ct.IsDelete==0
                                                      select ct).ToList();

            dgvDanhSachPhieuNhapKho.DataSource = lstCTPhieuNhapKho;
        }

        public FrmPhieuNhapKho(MuaHang muahang, NhanVien nv)
        {
            InitializeComponent();

            dgvDanhSachPhieuNhapKho.AutoGenerateColumns = false;
            dgvDanhSachPhieuNhapKho.ReadOnly = true;

            this.NhanVien = nv;
            this.muahang = muahang;

            ResultObj rs = new ResultObj();
            rs = chinhanhctyMgt.GetAllChiNhanhCongTy();

            ChiNhanhCongTy chinhanh = (from cn in (List<ChiNhanhCongTy>)rs.Result
                                       where cn.Id == this.muahang.IdChiNhanhCongTy &&
                                       cn.IsDelete == 0
                                       select cn).FirstOrDefault();
            btnLuu.Enabled = false;
            gbPhieuNhapKho.Enabled = false;

            txtSoPhieu.Text = this.muahang.MaSo;
            dtpNgayThang.Value = this.muahang.NgayThang;
            txtNguoiLap.Text = this.muahang.NguoiLap;
            txtNoiDung.Text = this.muahang.DienGiai;
            txtNguoiGiaoHang.Text = this.muahang.NguoiGiaoHang;
            txtKeToanTruong.Text = this.muahang.NguoiLap;
            txtSoHoaDon.Text = this.muahang.ChungTuKemTheo;
            txtVAT.Text = this.muahang.VAT.ToString();
            lblId.Text = this.muahang.Id.ToString();
            cbKhoNhap.Text = chinhanh.TenChiNhanh;
            IdBK = this.muahang.IsNhapKho;

            rs = ctmuahangMgt.GetAllCTMuaHang();
            List<CTMuaHang> lstCTMuaHang = (from ct in (List<CTMuaHang>)rs.Result
                                                      where ct.IdMuaHang == muahang.Id &&
                                                      ct.IsDelete == 0
                                                      select ct).ToList();

            dgvDanhSachPhieuNhapKho.DataSource = lstCTMuaHang;
        }

        #endregion

        #region Xu ly Button
        private void btnLuu_Click(object sender, EventArgs e)
        {
            ResultObj resultObj = new ResultObj();
            phieunhapkho = new PhieuNhapKho();

            if (txtNoiDung.Text == "" || txtSoHoaDon.Text == "")
            {
                MessageBox.Show("Vui lòng nhập dữ liệu đầy đủ !", "Thông báo");
                return;
            }            

            phieunhapkho.MaSo = txtSoPhieu.Text;
            phieunhapkho.NguoiGiaoHang = txtNguoiGiaoHang.Text;
    
            phieunhapkho.NoiDung = txtNoiDung.Text;
            phieunhapkho.SoHoaDonDo = txtSoHoaDon.Text;
            
            phieunhapkho.NgayThang = dtpNgayThang.Value;
            phieunhapkho.NguoiLap = txtNguoiLap.Text;
            phieunhapkho.KeToanTruong = txtKeToanTruong.Text;
            phieunhapkho.Thue = Convert.ToInt32(txtVAT.Text);
           
            if (!IsEdited)
            {
                phieunhapkho.IsDelete = 0;
                phieunhapkho.IdBangKe = BangKeNhapHang.Id;
                phieunhapkho.IdChiNhanhCTy = Convert.ToInt32(cbKhoNhap.SelectedValue.ToString());

                resultObj = phieunhapkhoMgt.SaveNewPhieuNhapKho(phieunhapkho);
                phieunhapkho.Id = resultObj.ReturnCode;

                List<PhieuNhapKho> lstPhieuNhapKho = new List<PhieuNhapKho>();
                lstPhieuNhapKho = GetAllPhieuNhapKhoByIdMaSo(phieunhapkho);

                for (int i = 0; i < dgvDanhSachPhieuNhapKho.Rows.Count; i++)
                {
                    CTPhieuNhapKho ctphieunhapkho = new CTPhieuNhapKho();

                    ctphieunhapkho.MaVatLieu = dgvDanhSachPhieuNhapKho.Rows[i].Cells[1].Value.ToString();
                    ctphieunhapkho.TenVatLieu = dgvDanhSachPhieuNhapKho.Rows[i].Cells[2].Value.ToString();
                    ctphieunhapkho.SoLuong = Convert.ToInt64(dgvDanhSachPhieuNhapKho.Rows[i].Cells[3].Value.ToString());
                    ctphieunhapkho.DonGia = Convert.ToInt64(dgvDanhSachPhieuNhapKho.Rows[i].Cells[4].Value.ToString());
                    ctphieunhapkho.ThanhTien = Convert.ToInt64(dgvDanhSachPhieuNhapKho.Rows[i].Cells[5].Value.ToString());
                    ctphieunhapkho.Tong = Convert.ToInt64(dgvDanhSachPhieuNhapKho.Rows[i].Cells[6].Value.ToString());
                    ctphieunhapkho.DonViTinh = dgvDanhSachPhieuNhapKho.Rows[i].Cells[7].Value.ToString();
                    ctphieunhapkho.IdPhieuNhapKho = phieunhapkho.Id;
                    ctphieunhapkho.IsDelete = 0;

                    resultObj = ctphieunhapkhoMgt.SaveNewCTPhieuNhapKho(ctphieunhapkho);
                }
            }
            else
            {                
                phieunhapkho.IdChiNhanhCTy = Convert.ToInt32(cbKhoNhap.SelectedValue.ToString());
                phieunhapkho.Id = Convert.ToInt32(lblId.Text);
                phieunhapkho.IdBangKe = IdBK;

                resultObj = phieunhapkhoMgt.UpdatePhieuNhapKho(phieunhapkho);

                CTPhieuNhapKho ctphieunhapkho = new CTPhieuNhapKho();
                for (int i = 0; i < dgvDanhSachPhieuNhapKho.Rows.Count; i++)
                {
                    ctphieunhapkho.Id = Convert.ToInt32(dgvDanhSachPhieuNhapKho.Rows[i].Cells[0].Value);
                    ctphieunhapkho.MaVatLieu = dgvDanhSachPhieuNhapKho.Rows[i].Cells[1].Value.ToString();
                    ctphieunhapkho.TenVatLieu = dgvDanhSachPhieuNhapKho.Rows[i].Cells[2].Value.ToString();
                    ctphieunhapkho.SoLuong = Convert.ToInt64(dgvDanhSachPhieuNhapKho.Rows[i].Cells[3].Value.ToString());
                    ctphieunhapkho.DonGia = Convert.ToInt64(dgvDanhSachPhieuNhapKho.Rows[i].Cells[4].Value.ToString());
                    ctphieunhapkho.ThanhTien = Convert.ToInt64(dgvDanhSachPhieuNhapKho.Rows[i].Cells[5].Value.ToString());
                    ctphieunhapkho.Tong = Convert.ToInt64(dgvDanhSachPhieuNhapKho.Rows[i].Cells[6].Value.ToString());
                    ctphieunhapkho.DonViTinh = dgvDanhSachPhieuNhapKho.Rows[i].Cells[7].Value.ToString();
                    ctphieunhapkho.IdPhieuNhapKho = phieunhapkho.Id;

                    if (dgvDanhSachPhieuNhapKho.Rows[i].Cells[6].Value != null)
                    {
                        ctphieunhapkho.Id = Convert.ToInt32(dgvDanhSachPhieuNhapKho.Rows[i].Cells[0].Value);
                        resultObj = ctphieunhapkhoMgt.UpdateCTPhieuNhapKho(ctphieunhapkho);
                    }
                    else
                    {
                        resultObj = ctphieunhapkhoMgt.SaveNewCTPhieuNhapKho(ctphieunhapkho);
                        dgvDanhSachPhieuNhapKho.Rows[i].Cells[0].Value = resultObj.ReturnCode;
                    }

                    resultObj = ctphieunhapkhoMgt.UpdateCTPhieuNhapKho(ctphieunhapkho);
                }
                IsEdited = false;
            }

            if (resultObj.ReturnCode > 0)
            {
                MessageBox.Show("Lưu thành công", "Thông báo");
                List<QuanLyKho> lstQuanLykho = (from q in GetAllQuanLyKho()
                                                where q.SoChungTuNhap == phieunhapkho.MaSo &&
                                                q.IsDelete==0
                                                select q).ToList();
                if (lstQuanLykho.Count <= 0)
                {
                    ResultObj resObj = new ResultObj();
                    List<Kho> lstKho = (from k in GetAllKho()
                                        where k.TenKho == "152"
                                        select k).ToList();
                    for (int i = 0; i < dgvDanhSachPhieuNhapKho.Rows.Count; i++)
                    {
                        string maso = dgvDanhSachPhieuNhapKho.Rows[i].Cells[0].Value.ToString();
                        List<HangHoa> lstHangHoa = (from h in GetAllHangHoa()
                                                    where h.MaSo == maso&&
                                                    h.IsDelete==0
                                                    select h).ToList();
                        if (lstKho.Count > 0 && lstHangHoa.Count > 0)
                        {
                            QuanLyKho quanlykho = new QuanLyKho();
                            quanlykho.IdKho = lstKho[0].Id;
                            quanlykho.IdHangHoa = lstHangHoa[0].Id;
                            quanlykho.SoChungTuNhap = phieunhapkho.MaSo;
                            quanlykho.SoChungTuXuat = "";

                            resObj = quanlykhoMgt.SaveNewQuanLyKho(quanlykho);
                        }
                    }
                }

                //delete cac CTPhieuNhapKho do nguoi dung chuot phai "XOA"
                if (lstDeleteCTPhieuNhapKho.Count > 0)
                {
                    foreach (CTPhieuNhapKho ct in this.lstDeleteCTPhieuNhapKho)
                    {
                        ct.IsDelete = 1;
                        ctphieunhapkhoMgt.UpdateCTPhieuNhapKho(ct);
                    }
                }

                btnDinhKhoan.Enabled = true;
                gbPhieuNhapKho.Enabled = false;
                btnLuu.Enabled = false;
                btnXoa.Enabled = true;
                btnSua.Enabled = true;
            }
            else if (resultObj.ReturnCode == Definitions.DUPLICATED_VALUE)
                MessageBox.Show("Dữ liệu bị trùng", "Thông báo");
            else
                MessageBox.Show("Thất bại");
            
        }

        private void btnIn_Click(object sender, EventArgs e)
        {
            tien = 0;
            double soTien = 0;
            long thanhtien = 0;
            long sumtien = 0;
            for (int i = 0; i < dgvDanhSachPhieuNhapKho.Rows.Count; i++)
            {
                thanhtien= Convert.ToInt32(dgvDanhSachPhieuNhapKho.Rows[i].Cells[5].Value);
                sumtien += thanhtien;
                soTien += thanhtien * phieunhapkho.Thue / 100;
            }
            tien = sumtien + Convert.ToInt64(soTien);

            string tienchu = CNum2Char.So2Chu((long)Convert.ToDouble(tien));
            FrmXemReport frmView = new FrmXemReport(this.phieunhapkho, tienchu,tien);
            this.Hide();
            frmView.ShowDialog();
            this.Show();
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            ResultObj result = new ResultObj();
            List<CTPhieuNhapKho> lstctPhieuNhapKho = (from ct in GetAllCTPhieuNhapKho()
                                                      where ct.IdPhieuNhapKho == phieunhapkho.Id
                                                      select ct).ToList();

            phieunhapkho.Id = Convert.ToInt32(lblId.Text);
            phieunhapkho.IsDelete = 1;
            result = phieunhapkhoMgt.UpdatePhieuNhapKho(phieunhapkho);

            for (int i = 0; i < lstctPhieuNhapKho.Count; i++)
            {
                lstctPhieuNhapKho[i].IsDelete = 1;
                result = ctphieunhapkhoMgt.UpdateCTPhieuNhapKho(lstctPhieuNhapKho[i]);
            }

            dgvDanhSachPhieuNhapKho.ClearSelection();

            txtKeToanTruong.Clear();
            txtNoiDung.Clear();
            txtNguoiGiaoHang.Clear();
            txtNguoiLap.Clear();
            txtSoHoaDon.Clear();
            txtSoPhieu.Clear();
            txtVAT.Clear();
            dtpNgayThang.Value = DateTime.Now;

            this.Close();
        }

        private void btnSua_Click(object sender, EventArgs e)
        {
            cbKhoNhap.DataSource = GetAllChiNhanhCongTy();
            cbKhoNhap.DisplayMember = "TenChiNhanh";
            cbKhoNhap.ValueMember = "Id";

            btnLuu.Enabled = true;
            btnXoa.Enabled = false;
            btnDinhKhoan.Enabled = false;
            btnIn.Enabled = false;
            btnSua.Enabled = false;

            IsEdited = true;
            gbPhieuNhapKho.Enabled = true;
            dgvDanhSachPhieuNhapKho.Enabled = true;
            dgvDanhSachPhieuNhapKho.ReadOnly = false;

            if (lblId.Text == "" )
            {
                lblId.Text = phieunhapkho.Id.ToString();
            }
            if (IdBK == 0)
            {
                IdBK = phieunhapkho.IdBangKe;
            }

            if (!IsEdit)
            {
                ResultObj result = new ResultObj();
                result = ctphieunhapkhoMgt.GetAllCTPhieuNhapKho();
                List<CTPhieuNhapKho> lstCTPhieuNhapKho = (from ct in (List<CTPhieuNhapKho>)result.Result
                                                          where ct.IdPhieuNhapKho == this.phieunhapkho.Id &&
                                                          ct.IsDelete==0
                                                          select ct).ToList();

                dgvDanhSachPhieuNhapKho.DataSource = lstCTPhieuNhapKho;
            }
        }

        private void btnDinhKhoan_Click(object sender, EventArgs e)
        {            
            PhieuNhapKho phieunhapkho = new PhieuNhapKho();
            phieunhapkho.MaSo = txtSoPhieu.Text;
            phieunhapkho.NoiDung = txtNoiDung.Text;
            phieunhapkho.chinhanhcongty = (ChiNhanhCongTy)cbKhoNhap.SelectedItem;
            phieunhapkho.SoHoaDonDo = txtSoHoaDon.Text;
            phieunhapkho.NgayThang = dtpNgayThang.Value;
            phieunhapkho.NguoiGiaoHang = txtNguoiGiaoHang.Text;
            phieunhapkho.Thue = Convert.ToInt32(txtVAT.Text);
            phieunhapkho.NguoiLap = txtNguoiLap.Text;
            phieunhapkho.KeToanTruong = txtKeToanTruong.Text;
        
            phieunhapkho.IdChiNhanhCTy = this.phieunhapkho.IdChiNhanhCTy;
            if (phieunhapkho.IdChiNhanhCTy.ToString() == "")
            {
                phieunhapkho.IdChiNhanhCTy = Convert.ToInt32(cbKhoNhap.SelectedValue.ToString());
            }
            phieunhapkho.listCTPhieuNhapKho = new List<CTPhieuNhapKho>();
            phieunhapkho.IdBangKe = IdBK;

            tien = 0;
            for (int i = 0; i < dgvDanhSachPhieuNhapKho.Rows.Count; i++)
            {
                tien += Convert.ToInt64(dgvDanhSachPhieuNhapKho.Rows[i].Cells[5].Value);
                CTPhieuNhapKho ctphieunhapkho = new CTPhieuNhapKho();
                ctphieunhapkho.ThanhTien = Convert.ToInt64(dgvDanhSachPhieuNhapKho.Rows[i].Cells[5].Value.ToString());
                phieunhapkho.listCTPhieuNhapKho.Add(ctphieunhapkho);
            }

            tien += Convert.ToInt64(Math.Ceiling((double)tien * phieunhapkho.Thue / 100));

            FrmDinhKhoanPhieuNhapKho frmDinhKhoanPhieuNhapKho = new FrmDinhKhoanPhieuNhapKho(phieunhapkho, tien);
            frmDinhKhoanPhieuNhapKho.BangKeNhapHang = this.bangkenhaphang;
            frmDinhKhoanPhieuNhapKho.ShowDialog();

            btnDinhKhoan.Enabled = false;
            btnIn.Enabled = true;
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        #endregion

        #region Xu ly cac Su kien
        private void txtVAT_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar))
            {
                e.Handled = true;
                MessageBox.Show("Vui lòng nhập số !");
            }
        }

        private void txtVAT_TextChanged(object sender, EventArgs e)
        {
            if (txtVAT.Text == "")
                txtVAT.Text = "0";
        }

        private void XoaToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (dgvDanhSachPhieuNhapKho.CurrentRow != null)
            {
                if (dgvDanhSachPhieuNhapKho.CurrentRow.Index != (dgvDanhSachPhieuNhapKho.Rows.Count))
                {
                    if (dgvDanhSachPhieuNhapKho.CurrentRow.Cells[0].Value != null)
                    {
                        int id = Convert.ToInt32(dgvDanhSachPhieuNhapKho.CurrentRow.Cells[0].Value);
                        CTPhieuNhapKho ctPhieuNhapKho = (from ct in GetAllCTPhieuNhapKho()
                                                         where ct.Id == id
                                                         select ct).FirstOrDefault();
                        if (ctPhieuNhapKho != null)
                        {
                            if (!lstDeleteCTPhieuNhapKho.Contains(ctPhieuNhapKho))
                                lstDeleteCTPhieuNhapKho.Add(ctPhieuNhapKho);

                        }
                    }

                    dgvDanhSachPhieuNhapKho.Rows.RemoveAt(dgvDanhSachPhieuNhapKho.CurrentRow.Index);
                }
            }
        }

        private void dgvDanhSachPhieuNhapKho_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
        {
            if (e.Button == System.Windows.Forms.MouseButtons.Right)
            {
                DataGridView.HitTestInfo hti = dgvDanhSachPhieuNhapKho.HitTest(e.X, e.Y);
                if (hti.Type == DataGridViewHitTestType.Cell)
                {
                    dgvDanhSachPhieuNhapKho.ClearSelection();
                    dgvDanhSachPhieuNhapKho.CurrentCell = dgvDanhSachPhieuNhapKho.Rows[e.RowIndex].Cells[e.ColumnIndex];
                    dgvDanhSachPhieuNhapKho.ContextMenuStrip = contextMenuStrip1;
                }
            }
        }

        public void LoadDataGridView()
        {
            List<CTBangKeNhapHang> lstCTBangKeNhapHang = new List<CTBangKeNhapHang>();
            List<HangHoa> lstHangHoa = new List<HangHoa>();
            List<CTPhieuNhapKho> lstCTPhieuNhapKho = new List<CTPhieuNhapKho>();

            lstHangHoa = GetAllHangHoa();
            CTBangKeNhapHang ctbangkenhaphang = new CTBangKeNhapHang();

            ctbangkenhaphang.IdBangKe = bangkenhaphang.Id;
            lstCTBangKeNhapHang = GetAllCTBangKeNhapHangByIdBangKe(ctbangkenhaphang);

            long soluong = 0;
            long thanhtien = 0;

            string mavatlieu = "";
            string tenvatlieu = "";
            string donvitinh = "";

            for (int i = 0; i < lstHangHoa.Count; i++)
            {
                for (int j = 0; j < lstCTBangKeNhapHang.Count; j++)
                {
                    if (lstHangHoa[i].MaSo.ToString() == lstCTBangKeNhapHang[j].MaVatLieu.ToString())
                    {
                        soluong += lstCTBangKeNhapHang[j].SoLuong;
                        thanhtien += lstCTBangKeNhapHang[j].SoLuong * lstCTBangKeNhapHang[j].DonGia;
                        mavatlieu = lstCTBangKeNhapHang[j].MaVatLieu;
                        tenvatlieu = lstCTBangKeNhapHang[j].TenVatLieu;
                        donvitinh = lstCTBangKeNhapHang[j].DonViTinh;
                    }

                    if (j == lstCTBangKeNhapHang.Count - 1)
                    {
                        if (soluong != 0)
                        {
                            CTPhieuNhapKho ctphieunhapkho = new CTPhieuNhapKho();

                            ctphieunhapkho.MaVatLieu = mavatlieu;
                            ctphieunhapkho.TenVatLieu = tenvatlieu;
                            ctphieunhapkho.DonViTinh = donvitinh;
                            ctphieunhapkho.SoLuong = soluong;
                            ctphieunhapkho.DonGia = thanhtien / soluong;
                            ctphieunhapkho.ThanhTien = thanhtien;
                            lstCTPhieuNhapKho.Add(ctphieunhapkho);
                        }
                    }
                }

                soluong = 0;
                thanhtien = 0;
            }

            dgvDanhSachPhieuNhapKho.AutoGenerateColumns = false;
            dgvDanhSachPhieuNhapKho.DataSource = lstCTPhieuNhapKho;
        }

        public string GetMaSoPhieuNhapKho()
        {
            List<PhieuNhapKho> lstPhieuNhapKho = new List<PhieuNhapKho>();
            ResultObj resultObj = new ResultObj();
            resultObj = phieunhapkhoMgt.GetAllPhieuNhapKhoToCheckCode();
            lstPhieuNhapKho = (List<PhieuNhapKho>)resultObj.Result;

            DateTime dt = DateTime.Now;
            string sResult = @"PNK/";

            int month = dt.Month;
            int year = dt.Year;

            string sNewnumber = "";
            string sMonth = month.ToString();
            string sYear = year.ToString();

            int newMaSo = 0;

            if (lstPhieuNhapKho.Count == 0)
                newMaSo = 1;
            else
            {
                //kiem tra da thay doi thang moi chua
                int index = lstPhieuNhapKho[lstPhieuNhapKho.Count - 1].MaSo.IndexOf('/');
                int test = Convert.ToInt32(lstPhieuNhapKho[lstPhieuNhapKho.Count - 1].MaSo.Substring(index + 1, 2).ToString());
                if (test != month)
                    newMaSo = 1;
                else
                {
                    index = lstPhieuNhapKho[lstPhieuNhapKho.Count - 1].MaSo.LastIndexOf('/');
                    int so = Convert.ToInt32(lstPhieuNhapKho[lstPhieuNhapKho.Count - 1].MaSo.Substring(index + 1).ToString());
                    newMaSo = so + 1;
                }
            }

            if (sMonth.Length == 1)
                sMonth = "0" + sMonth;

            sYear = sYear.Substring(2);

            string sId = newMaSo.ToString();
            if (sId.Length == 1)
                sNewnumber = "0000" + sId;
            else if (sId.Length == 2)
                sNewnumber = "000" + sId;
            else if (sId.Length == 3)
                sNewnumber = "00" + sId;
            else if (sId.Length == 4)
                sNewnumber = "0" + sId;
            else
                sNewnumber = sId;

            sResult += sMonth + sYear + "/" + sNewnumber;

            return sResult;
        }
        #endregion

        #region Lay du lieu
        public List<ChiNhanhCongTy> GetAllChiNhanhCongTy()
        {
            ResultObj resultObj = new ResultObj();

            List<ChiNhanhCongTy> lstChiNhanhCongTy = new List<ChiNhanhCongTy>();

            resultObj = chinhanhctyMgt.GetAllChiNhanhCongTy();
            lstChiNhanhCongTy =(from cn in(List<ChiNhanhCongTy>)resultObj.Result
                                    where cn.IsDelete==0
                                    select cn).ToList();

            return lstChiNhanhCongTy;
        }

        public List<BangKeNhapHang> GetAllBangKeNhapHang()
        {
            ResultObj resultObj = new ResultObj();

            List<BangKeNhapHang> lstBangKeNhapHang = new List<BangKeNhapHang>();

            resultObj = bangkenhaphangMgt.GetAllBangKeNhapHang();
            lstBangKeNhapHang = (from bk in(List<BangKeNhapHang>)resultObj.Result
                                     where bk.IsDelete ==0
                                     select bk).ToList();

            return lstBangKeNhapHang;
        }

        public List<PhieuNhapKho> GetAllPhieuNhapKho()
        {
            ResultObj resultObj = new ResultObj();

            List<PhieuNhapKho> lstPhieuNhapKho = new List<PhieuNhapKho>();

            resultObj = phieunhapkhoMgt.GetAllPhieuNhapKho();
            lstPhieuNhapKho = (from pnk in(List<PhieuNhapKho>)resultObj.Result
                                   where pnk.IsDelete ==0
                                   select pnk).ToList();

            return lstPhieuNhapKho;
        }

        public List<CTBangKeNhapHang> GetAllCTBangKeNhapHang()
        {
            ResultObj resultObj = new ResultObj();

            List<CTBangKeNhapHang> lstCTBangKeNhapHang = new List<CTBangKeNhapHang>();

            resultObj = ctbankenhaphangMgt.GetAllCTBangKeNhapHang();
            lstCTBangKeNhapHang = (from ct in(List<CTBangKeNhapHang>)resultObj.Result
                                       where ct.IsDelete==0
                                       select ct).ToList();

            return lstCTBangKeNhapHang;
        }

        public List<CTBangKeNhapHang> GetAllCTBangKeNhapHangByIdBangKe(CTBangKeNhapHang ctbangkenhaphang)
        {
            ResultObj resultObj = new ResultObj();

            List<CTBangKeNhapHang> lstCTBangKeNhapHang = new List<CTBangKeNhapHang>();

            resultObj = ctbankenhaphangMgt.GetAllCTBangKeNhapHangByIdBangKe(ctbangkenhaphang);
            lstCTBangKeNhapHang = (from ct in(List<CTBangKeNhapHang>)resultObj.Result
                                       where ct.IsDelete==0
                                       select ct).ToList();

            return lstCTBangKeNhapHang;
        }

        public List<PhieuNhapKho> GetAllPhieuNhapKhoByIdMaSo(PhieuNhapKho phieunhapkho)
        {
            ResultObj resultObj = new ResultObj();

            List<PhieuNhapKho> lstPhieuNhapKho = new List<PhieuNhapKho>();

            resultObj = phieunhapkhoMgt.GetPhieuNhapKhoByMaSo(phieunhapkho);
            lstPhieuNhapKho = (from pnk in(List<PhieuNhapKho>)resultObj.Result
                                   where pnk.IsDelete==0
                                   select pnk).ToList();

            return lstPhieuNhapKho;
        }

        public List<HangHoa> GetAllHangHoa()
        {
            ResultObj resultObj = new ResultObj();

            List<HangHoa> lstHangHoa = new List<HangHoa>();

            resultObj = hanghoaMgt.GetAllHangHoa();
            lstHangHoa = (from hh in(List<HangHoa>)resultObj.Result
                              where hh.IsDelete==0
                              select hh).ToList();

            return lstHangHoa;
        }

        public List<Kho> GetAllKho()
        {
            ResultObj result = new ResultObj();
            result = khoMgt.GetAllKho();
            List<Kho> lstKho = ((List<Kho>)result.Result);

            return lstKho;
        }

        public List<QuanLyKho> GetAllQuanLyKho()
        {
            ResultObj result = new ResultObj();
            result = quanlykhoMgt.GetAllQuanLyKho();
            List<QuanLyKho> lstQLKho = (from ql in(List<QuanLyKho>)result.Result
                                            where ql.IsDelete==0
                                            select ql).ToList();
            return lstQLKho;
        }

        public List<CTPhieuNhapKho> GetAllCTPhieuNhapKho()
        {
            ResultObj result = new ResultObj();
            result = ctphieunhapkhoMgt.GetAllCTPhieuNhapKho();
            List<CTPhieuNhapKho> lstctPhieuNhapKho = (from ct in (List<CTPhieuNhapKho>)result.Result
                                                      where ct.IsDelete == 0
                                                      select ct).ToList();
            return lstctPhieuNhapKho;
        }
        #endregion
    }
}